package com.cityos.android.mystudynestedscroll.ui.view

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.ViewConfiguration
import androidx.appcompat.widget.AppCompatImageView
import androidx.core.view.ViewCompat

/**
 * @author hailong
 * @date 2024年02月19日 11:28
 *
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 */
class CustomView @JvmOverloads constructor(context: Context , attrs: AttributeSet? = null) : AppCompatImageView(context , attrs) {

    var lastX: Float = 0f
    var lastY: Float = 0f

    val scaledTouchSlop = ViewConfiguration.get(context).scaledTouchSlop
    override fun onTouchEvent(event: MotionEvent): Boolean {
        super.onTouchEvent(event)
        if (event.action == MotionEvent.ACTION_DOWN) {
            lastX = event.getX(0)
            lastY = event.getY(0)
        }

        if (event.action == MotionEvent.ACTION_MOVE) {
            val currentX = event.getX(0)
            val currentY = event.getY(0)
            val deltaX = currentX - lastX
            val deltaY = currentY - lastY

            ViewCompat.offsetTopAndBottom(this , deltaY.toInt())
            ViewCompat.offsetLeftAndRight(this , deltaX.toInt())

            Log.e("hailong" , "${this.left} ${this.top}  lastX=$lastX currentX=$currentX lastY=$lastY  currentY=$currentY ");

        }

        if (event.action in arrayOf(MotionEvent.ACTION_UP , MotionEvent.ACTION_CANCEL)) {
        }

        return true
    }


}